//
//  ChatGiftTableViewCell.m
//  Molian
//
//  Created by xiaocankeji on 15/8/12.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "ChatGiftTableViewCell.h"
#import "EMChatVideoBubbleView.h"
#import "UIResponder+Router.h"


@interface ChatGiftTableViewCell()
{
    MessageModel *_model;
}
@end


@implementation ChatGiftTableViewCell

- (id)initWithMessageModel:(MessageModel *)model reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithMessageModel:model reuseIdentifier:reuseIdentifier];
    if (self) {
        _model = model;
        // Initialization code
        self.headImageView.clipsToBounds = YES;
        self.headImageView.layer.cornerRadius = 3.0;
    }
    return self;
}
- (BOOL)canBecomeFirstResponder
{
    return YES;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGRect bubbleFrame = _bubbleView.frame;
    bubbleFrame.origin.y = self.headImageView.frame.origin.y;
    NSDictionary *dic =  _model.message.ext;
    
    BOOL isReceiver = !_model.isSender;
//    NSString *imageName;
//    if([_model.message.to isEqualToString:[MLUser currentUser].objectId]){
//         imageName =   BUBBLE_LEFT_IMAGE_NAME;
//    }else{
//        imageName = BUBBLE_RIGHT_IMAGE_NAME;
//    }
    NSString *imageName = isReceiver ? kChat_Gitf_Receiver_Bg : kChat_Gift_Sender_Bg;
    NSInteger leftCapWidth = isReceiver?BUBBLE_LEFT_LEFT_CAP_WIDTH:BUBBLE_RIGHT_LEFT_CAP_WIDTH;
    NSInteger topCapHeight =  isReceiver?BUBBLE_LEFT_TOP_CAP_HEIGHT:BUBBLE_RIGHT_TOP_CAP_HEIGHT;
    _giftBackgroudImageView.image = [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
    
    
    [self.giftImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"giftImg"]] placeholderImage:[UIImage imageNamed:@"chat_gift_icon"]];
    
    
    self.giftNameLabel.text = [dic objectForKey:@"giftName"];
    self.giftInfoLabel.text = [dic objectForKey:@"giftInfo"];
    if (self.messageModel.isSender) {
        bubbleFrame.origin.y = self.headImageView.frame.origin.y;
        // 菊花状态 （因不确定菊花具体位置，要在子类中实现位置的修改）
        //未读
        _hasReadImage.hidden = YES;
        switch (self.messageModel.status) {
            case eMessageDeliveryState_Delivering:
            {
                
                [_messageType setHidden:YES];
                
                [_activityView setHidden:NO];
                [_activtiy startAnimating];
                //[_retryButton setHidden:YES];
            }
                break;
            case eMessageDeliveryState_Delivered:
            {
                [_activtiy stopAnimating];
             //   [_retryButton setHidden:YES];
                [_messageType setHidden:NO];
                [_activityView setHidden:NO];
                if (self.messageModel.message.isReadAcked)
                {
                    //已读
                    // _hasRead.text = @"";
                    
                    _hasReadImage.hidden = YES;
                    //_hasRead.backgroundColor = [UIColor clearColor];
                }else{
                    _hasReadImage.hidden = NO;
                }
                
                 if ([[self.messageModel.message.ext objectForKey:@"is_voice_call"]isEqual:@true]){
                    //  _activityView.hidden = NO;
                    _messageType.frame = CGRectMake(0, 0, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
                    _messageType.image = [UIImage imageNamed:@"news_chat_missed-calls_icon"];
                    
                }else if ([[self.messageModel.message.ext objectForKey:@"is_voice_call"]isEqual:@false]){
                    // _activityView.hidden = NO;
                    _messageType.frame = CGRectMake(0, 0, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
                    _messageType.image = [UIImage imageNamed:@"news_chat_-call-time_icon"];
                    
                }else{
                    //  _activityView.hidden = NO;
                    _messageType.frame = CGRectMake(0, 0, 0, 0);
                    _messageType.image = [UIImage imageNamed:@"chat_clear_color"];
                }
                
            }
                break;
            case eMessageDeliveryState_Pending:
            case eMessageDeliveryState_Failure:
            {
                _messageType.frame = CGRectMake(0, 0, 0, 0);
                [_activtiy stopAnimating];
                [_messageType setHidden:YES];
             //   [_retryButton setHidden:NO];
                [_activityView setHidden:NO];
                
            }
                break;
            default:
                break;
        }
        
        
        bubbleFrame.origin.x = self.headImageView.frame.origin.x - bubbleFrame.size.width - HEAD_PADDING;
        _bubbleView.frame = bubbleFrame;
        
        CGRect frame = self.activityView.frame;
        frame.origin.x = bubbleFrame.origin.x - frame.size.width - ACTIVTIYVIEW_BUBBLE_PADDING;
        frame.origin.y = _bubbleView.center.y - frame.size.height / 2;
        self.activityView.frame = frame;
        
    }
    else{
        bubbleFrame.origin.x = HEAD_PADDING * 2 + HEAD_SIZE;
            bubbleFrame.size.width = bubbleFrame.size.width;
            _bubbleView.frame = bubbleFrame;
        
         if ([[self.messageModel.message.ext objectForKey:@"type"]isEqualToString:EMMESSAGE_EXT_TYPE_BURN_AFTER_READ_MSG]){
            _messageType.frame = CGRectMake(_bubbleView.frame.size.width + (HEAD_PADDING * 2 + HEAD_SIZE) +5, SEND_STATUS_SIZE/2, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
            _messageType.image = [UIImage imageNamed:@"chat_simi"];
            
        }else if ([[self.messageModel.message.ext objectForKey:@"is_voice_call"]isEqual:@true]){
            _messageType.frame = CGRectMake(_bubbleView.frame.size.width + (HEAD_PADDING * 2 + HEAD_SIZE) +5, SEND_STATUS_SIZE/2, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
            _messageType.image = [UIImage imageNamed:@"news_chat_missed-calls_icon"];
        }else if ([[self.messageModel.message.ext objectForKey:@"is_voice_call"]isEqual:@false]){
            _messageType.frame = CGRectMake(_bubbleView.frame.size.width + (HEAD_PADDING * 2 + HEAD_SIZE) +5, SEND_STATUS_SIZE/2, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
            _messageType.image = [UIImage imageNamed:@"news_chat_-call-time_icon"];
            
        }else{
            _messageType.image = [UIImage imageNamed:@"chat_clear_color"];
            _messageType.frame = CGRectMake(0, 0, 0, 0);
        }
        
    }
}
//+ (CGFloat)bubbleViewHeightForMessageModel:(MessageModel *)messageModel
//{
//    return 80;
//}
//+ (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath withObject:(MessageModel *)model
//{
//    return 200;
//    
//}
- (void)awakeFromNib {
    // Initialization code
}

-(void)BurnafterreadingHidden:(UITapGestureRecognizer*)sender
{
    //    [sender.view setHidden:YES];
}

//- (void)setMessageModel:(MessageModel *)model
//{
//    [super setMessageModel:model];
//    
//    if (model.messageType != eMessageTypeChat) {
//        _nameLabel.text = model.nickName;
//        _nameLabel.hidden = model.isSender;
//    }
//    
//    _bubbleView.model = self.messageModel;
//    [_bubbleView sizeToFit];
//}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

#pragma mark - action

//// 重发按钮事件
//-(void)retryButtonPressed:(UIButton *)sender
//{
//    [self routerEventWithName:kResendButtonTapEventName
//                     userInfo:@{kShouldResendCell:self}];
//}

#pragma mark - private

- (void)setupSubviewsForMessageModel:(MessageModel *)messageModel
{
    [super setupSubviewsForMessageModel:messageModel];
    
    //类型
    _messageType = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SEND_STATUS_SIZE, SEND_STATUS_SIZE)];
    _messageType.hidden = YES;
    
    if (messageModel.isSender) {
        
        
        // 发送进度显示view
        _activityView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SEND_STATUS_SIZE, SEND_STATUS_SIZE)];
        [_activityView setHidden:YES];
        [self.contentView addSubview:_activityView];
        [_activityView addSubview:_messageType];
        
        // 重发按钮
//        _retryButton = [UIButton buttonWithType:UIButtonTypeSystem];
//        _retryButton.frame = CGRectMake( 0, 0, SEND_STATUS_SIZE, SEND_STATUS_SIZE);
//        [_retryButton addTarget:self action:@selector(retryButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
//        //        [_retryButton setImage:[UIImage imageNamed:@"messageSendFail.png"] forState:UIControlStateNormal];
//        [_retryButton setBackgroundImage:[UIImage imageNamed:@"messageSendFail"] forState:UIControlStateNormal];
//        //[_retryButton setBackgroundColor:[UIColor redColor]];
//        [_activityView addSubview:_retryButton];
        
        // 菊花
        _activtiy = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _activtiy.backgroundColor = [UIColor clearColor];
        [_activityView addSubview:_activtiy];
        
        //已读
        
        _hasReadImage = [[UIImageView alloc] initWithFrame:CGRectMake(SEND_STATUS_SIZE-9-5, SEND_STATUS_SIZE/2.0-9/2.0, 9, 9)];
        _hasReadImage.image = [UIImage imageNamed:@"news_chat_unread_icon"];
        _hasReadImage.hidden = YES;
        [_hasReadImage setContentMode:UIViewContentModeScaleAspectFill];
        [_activityView addSubview:_hasReadImage];
        
        
        
    }else{
        [self.contentView addSubview:_messageType];
    }
    
    
    _bubbleView = [[EMChatBaseBubbleView alloc] initWithFrame:CGRectMake(0, 0, 230, 80)];
    _bubbleView.userInteractionEnabled = NO;
    
    self.giftBackgroudImageView = [[UIImageView alloc] initWithFrame:_bubbleView.frame];
    _giftBackgroudImageView.userInteractionEnabled = NO;
    [_bubbleView addSubview:_giftBackgroudImageView];
    self.giftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
    [_giftBackgroudImageView addSubview:_giftImageView];
    
    self.giftNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 13, 100, 20)];
    _giftNameLabel.font = kFont(16);
    _giftNameLabel.textColor = APPCOLOR(1);
    [_giftBackgroudImageView addSubview:_giftNameLabel];
    
    self.giftInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 40, 140, 20)];
    _giftInfoLabel.font = kFont(14);
    _giftInfoLabel.adjustsFontSizeToFitWidth = YES;
    _giftInfoLabel.textColor = RGBA(84, 85, 86, 1);
    [_giftBackgroudImageView addSubview:_giftInfoLabel];
    
    [self.contentView addSubview:_bubbleView];
    
    
   
    
    
//    _BurnAfterRreadingLabel = [[UILabel alloc]initWithFrame:_bubbleView.frame];
//    _BurnAfterRreadingLabel.text = @"  点击查看私密消息";
//    _BurnAfterRreadingLabel.numberOfLines = 0;
//    _BurnAfterRreadingLabel.lineBreakMode = NSLineBreakByCharWrapping;
//    _BurnAfterRreadingLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE];
//    _BurnAfterRreadingLabel.backgroundColor = RGBA(209, 224, 225, 1);
//    _BurnAfterRreadingLabel.userInteractionEnabled = NO;
//    _BurnAfterRreadingLabel.multipleTouchEnabled = NO;
//    [_bubbleView addSubview:_BurnAfterRreadingLabel];
//    [_BurnAfterRreadingLabel setHidden:YES];
    
}




@end
